package com.ponhu.ea2.stockcenter.controller;

import com.ponhu.ea2.stockcenter.dto.WMSEventInfoDTO;
import com.ponhu.ea2.stockcenter.dto.WMSInventoryInfo;
import com.ponhu.ea2.stockcenter.enums.DefinedSystem;
import com.ponhu.ea2.stockcenter.enums.ExchangeActionTypeEnum;
import com.ponhu.ea2.stockcenter.service.bms.StockCommonService;
import com.ponhu.ea2.stockcenter.service.bms.StockForWMSService;
import com.ponhu.ea2.stockcenter.vo.RealityLevelStockInfoVO;
import com.ponhu.ea2.stockcenter.vo.ReturnResponseBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/stock/wms")
@Api(tags = "库存对接WMS接口测试", value = "StockWmsTestController")
public class StockWmsTestController {

    @DubboReference(version = "1.0.0", check = false, timeout = 3600000)
    private StockForWMSService stockForWMSService;

    @DubboReference(version = "1.0.0", check = false)
    private StockCommonService stockCommonService;

    @GetMapping(value = "/actionTestPressure")
    @ApiOperation(value = "对接WMS系统事件处理接口-压力测试")
    public ReturnResponseBean actionTest(
            @RequestParam("actionType") ExchangeActionTypeEnum actionType,
            @RequestParam("inWarehouseCode") String inWarehouseCode,
            @RequestParam("inWarehouseIsSale") Boolean inWarehouseIsSale,
            @RequestParam("size") int size) {

        String stockRequestToken = stockCommonService
                .getStockRequestToken(DefinedSystem.WMS).getData();

        List<WMSInventoryInfo> inventoryInfos = stockForWMSService.getAllStockInventoryReal(size)
                .stream().map(inventoryCode ->
                        new WMSInventoryInfo().setInventoryCode(inventoryCode).setInventoryQuantity(1))
                .collect(Collectors.toList());

        WMSEventInfoDTO eventInfoDTO = new WMSEventInfoDTO()
                .setActionType(actionType)
                .setInWarehouseCode(inWarehouseCode)
                .setInWarehouseIsSale(inWarehouseIsSale)
                .setInventoryInfoList(inventoryInfos);

        long start = System.currentTimeMillis();
        ReturnResponseBean returnResponseBean = stockForWMSService
                .receiveWMSEventHandler(stockRequestToken, eventInfoDTO);
        System.out.printf("\033[32m 对接WMS系统事件处理接口-压力测试 ===>> 耗时：%s ms \033[0m\n",
                System.currentTimeMillis() - start);

        return returnResponseBean;
    }

    @PostMapping(value = "/actionTest")
    @ApiOperation(value = "对接WMS系统事件处理接口")
    public ReturnResponseBean actionTest(
            @RequestBody WMSEventInfoDTO eventInfoDTO) {
        String stockRequestToken = stockCommonService
                .getStockRequestToken(DefinedSystem.WMS).getData();

        return stockForWMSService
                .receiveWMSEventHandler(stockRequestToken, eventInfoDTO);
    }

    @GetMapping(value = "/getRealityStockInfo")
    @ApiOperation(value = "根据货品编码查询货品仓库库存信息")
    public ReturnResponseBean<List<RealityLevelStockInfoVO>> getRealityStockInfo(
            @RequestParam("inventoryCodeList") List<String> inventoryCodeList) {
        return stockForWMSService.getRealityStockInfo(inventoryCodeList);
    }

    @GetMapping(value = "/updTraceDone")
    @ApiOperation(value = "根号业务单号(链路号)关闭任务链路")
    public ReturnResponseBean updTraceDone(@RequestParam("docNo") String docNo) {
        return stockForWMSService.updTraceDone(docNo);
    }

}
